﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;
using CardsServer.Tools;
namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        List<View_cards> View_cards_Get(string state, string clientname, string ckname, string cno, int ACID);

        [OperationContract]
        List<CardOpenRQ_M> CardOpenRQ_M_ByClientID_Get(Guid? clientid,int? state, int ACID);

        [OperationContract]
        List<CardOpenRQ_D> CardOpenRQ_D_Bypid_Get(string pid, int ACID);

        [OperationContract]
        List<CardOpen_D> CardOpen_D_Bypid_Get(string pid, int ACID);

        [OperationContract]
        List<Cards> Get_BlankCards_ckandclient(Guid clientID, string WarehouseID, string goodcode, int ACID);

        [OperationContract]
        List<GetnumberMonth_CanSaleResult> cansalenumber_Get(int comboid,  DateTime activedate, decimal yue, int ACID);

        [OperationContract]
        List<GetnumberNotMonth_CanSaleResult> cansalenumberNotMonth_Get(int comboid, decimal yue, int ACID);

        [OperationContract]
        string openCard(CardOpen_M m,List<CardOpen_D> ds,int ACID);

        [OperationContract]
        List<Cards> Get_BlankCards_ck(string WarehouseID, string goodcode, int ACID);

        [OperationContract]
        List<Cards> Get_BlankCards_ClientID(Guid clientID, string goodcode, int ACID);

        [OperationContract]
        List<View_opencard_m> View_opencard_m_list(DateTime? sd, DateTime? ed, Guid? opid, Guid? jsid, Guid? clientid,int ACID);
    }
    public partial class Service : IService
    {

        public List<View_opencard_m> View_opencard_m_list(DateTime? sd, DateTime? ed, Guid? opid, Guid? jsid, Guid? clientid,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<View_opencard_m, bool>> predicate = PredicateBuilder.True<View_opencard_m>();
                if (ed!=null&&sd!=null)
                {
                    predicate = p => p.Pdate.Date>=sd&&p.Pdate<=ed;
                }
                if (opid.HasValue)
                {
                    predicate = predicate.And(p => p.opID == opid);
                }
                                
                if (jsid.HasValue)
                {
                    predicate = predicate.And(p => p.JSempid == jsid);
                }

                if (clientid.HasValue)
                {
                    predicate = predicate.And(p => p.ClientID == clientid);
                }
                return ctx.View_opencard_m.Where(predicate).ToList();
            }
        }

        public List<View_cards> View_cards_Get(string state, string clientname, string ckname, string cno,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {

                Expression<Func<View_cards, bool>> predicate = PredicateBuilder.True<View_cards>();
                if (!string.IsNullOrEmpty(cno))
                {
                    predicate=p => p.串号 == cno;
                }
                if (!string.IsNullOrEmpty(state))
                {
                    predicate = predicate.And(p => p.状态 == state);
                }
                if (!string.IsNullOrEmpty(clientname))
                {
                    predicate = predicate.And(p => p.客户名称 == clientname);
                }
                if (!string.IsNullOrEmpty(ckname))
                {
                    predicate = predicate.And(p => p.仓库 == ckname);
                }
                return ctx.View_cards.Where(predicate).ToList();
            }
        }

        /// <summary>
        /// 取指定客户与自己指定仓库库存的空卡
        /// </summary>
        /// <param name="clientID"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<Cards> Get_BlankCards_ckandclient(Guid clientID, string WarehouseID,string goodcode, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
               return  ctx.Get_BlankCards_ckandclient(clientID,WarehouseID,goodcode).ToList();
                
            }
        }

        /// <summary>
        /// 取指定客户的空卡
        /// </summary>
        /// <param name="clientID"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<Cards> Get_BlankCards_ClientID(Guid clientID, string goodcode, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.Get_BlankCards_ClientID(clientID, goodcode).ToList();

            }
        }

        /// <summary>
        /// 取指定指定仓库库存的空卡
        /// </summary>
        /// <param name="clientID"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<Cards> Get_BlankCards_ck( string WarehouseID, string goodcode, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.Get_BlankCards_ck(WarehouseID, goodcode).ToList();

            }
        }

        /// <summary>
        /// 开卡
        /// </summary>
        /// <param name="clientid"></param>
        /// <param name="state"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<CardOpenRQ_M> CardOpenRQ_M_ByClientID_Get(Guid? clientid, int? state, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<CardOpenRQ_M, bool>> predicate = PredicateBuilder.True<CardOpenRQ_M>();
                if (clientid!=null)
                {
                    predicate.And(p => p.ClientID == clientid);
                }
                if (state!=null)
                {
                    predicate.And(p => p.state == state);
                }
                return ctx.CardOpenRQ_M.Where(predicate).ToList();
            }
        }

        public List<CardOpen_D> CardOpen_D_Bypid_Get(string pid, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.CardOpen_D.Where(p => p.pid == pid).ToList();
            }
        }
        public List<CardOpenRQ_D> CardOpenRQ_D_Bypid_Get(string pid, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.CardOpenRQ_D.Where(p => p.pid == pid).ToList();
            }
        }

        public List<GetnumberMonth_CanSaleResult> cansalenumber_Get(int comboid,DateTime activedate, decimal yue,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.GetnumberMonth_CanSale(comboid, activedate, yue).ToList();
            }
        }

        public List<GetnumberNotMonth_CanSaleResult> cansalenumberNotMonth_Get(int comboid,decimal yue,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.GetnumberNotMonth_CanSale(comboid, yue).ToList();
            }
        }

        public string openCard(CardOpen_M m, List<CardOpen_D> ds, int ACID)
        { 
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        string r1 = CardOpen_M_Add(m, ACID, ctx);
                        if (!string.IsNullOrEmpty(r1))
                        {
                            throw new Exception(r1);
                        }
                        
                        
                        foreach (CardOpen_D d in ds)
                        {
                            d.pid = m.pid;
                            string r2 = CardOpen_D_Add(d, ACID, ctx);

                            if (!string.IsNullOrEmpty(r2))
                            {
                                throw new Exception(r2);
                            }
                            //此处待斟酌，未区分非包月卡处理
                            Cards card = ctx.Cards.Single(p => p.cno == d.cno);
                                #region 更新库存卡片状态
                                if (card.ClientID == null)
                                {
                                    onhand o = ctx.onhand.Single(p => p.GoodsCode == card.GoodsCode && p.WarehouseID == card.WarehouseID);
                                    o.onhandcount -= 1;
                                    card.ClientID = m.ClientID;
                                    card.state = 1;
                                    card.WarehouseID = "";
                                }
                                #endregion
                            numberpool phone = ctx.numberpool.Single(p => p.phone == d.phone);
                            if (d.monthflag)
                            {

                                #region 更新号码状态
                                phone.activedate = d.activeDate;
                                phone.balance -= d.yue;
                                phone.cno = d.cno;
                                Combo combo = ctx.Combo.Single(p => p.ComboID == d.ComboID);
                                phone.exceeddate = d.activeDate.AddMonths(Convert.ToInt32(d.yue / combo.TCZ));
                                phone.state = 1;
                                #endregion
                            }
                            else
                            {
                                phone.activedate = d.activeDate;
                                phone.balance = 0;
                                phone.cno = d.cno;
                                phone.state = 1;
                            }
                            RecordBalance(m.total, m.ClientID, ctx, ACID);
                        }
                        ctx.SubmitChanges();
                        tc.Complete();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }


    }


}

